package cn.hackcloud.annotation.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class LinuxCondition implements Condition {
	/**
	 * ConditionContext 判断条件能使用的上下文 AnnotatedTypeMetadata 注释信息
	 */
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		// 是否linux系统
		// 1.能获取到IOC使用的Bean工厂
		ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
		// 2能获取到类加载器
		ClassLoader classLoader = context.getClassLoader();
		// 3 能获取到环境信息
		Environment environment = context.getEnvironment();
		// 4能获取到bean的定义注册类
		BeanDefinitionRegistry registry = context.getRegistry();
		String osName = environment.getProperty("os.name");
		if (osName.contains("Linux")) {
			return true;
		}
		return false;
	}

}
